<template>
  <div class="login-container">
    <div class="login-box">
      <div style="padding: 20px; background-color: white; margin-right:  20px; border-radius: 5px ">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 400px">
          <div style="margin-bottom: 30px; font-size: 24px; text-align: center; color: blue; font-weight: bold">
            欢迎来到后台登陆系统
          </div>

          <el-form-item prop="username">
            <el-input size="large" v-model="data.form.username" placeholder="请输入账号" prefix-icon="User"></el-input>
          </el-form-item>

<!--    show-password 密码输入时不显示      -->
          <el-form-item prop="password">
            <el-input size="large" show-password  v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input>
          </el-form-item>

          <el-form-item prop="role">
            <el-select style="width: 100%" size="large" v-model="data.form.role">
              <el-option value="ADMIN" label="管理员"></el-option>
              <el-option value="EMP" label="员工"></el-option>
            </el-select>
          </el-form-item>

          <div style="margin-bottom: 20px">
            <el-button @click="login" size="large" style="width: 100%;" type="primary">登录</el-button>
          </div>

          <div style="text-align: right">
            还没有账号？请<a href="/register" style="color: blueviolet; text-decoration: none">注册</a>
          </div>

        </el-form>
      </div>
    </div>
  </div>
</template>


<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";


const data = reactive({
  form:{
    role:'EMP'
  },
  rules:{
    username:[
      { required:true, message:'请输入账号',trigger:'blur' }
    ],
    password:[
      { required:true, message:'请输入密码',trigger:'blur' }
    ]
  }
})

const formRef = ref()

const login = () =>{
  formRef.value.validate((valid) =>{
    if (valid){
      request.post('/login',data.form).then(res =>{
        if (res.code === 200){  //登陆成功

          //   存储后台返回的用户信息
          localStorage.setItem('xm-pro-user',JSON.stringify(res.data))      //把json对象转换成json字符串存储

          ElMessage.success('登陆成功')
          setTimeout(() =>{
            location.href = '/manager/home'    //跳转页面
          },500)

        }else {
          ElMessage.error("账号或者密码错误")
        }
      })
    }
  })
}

</script>

<style scoped>
.login-container{
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/logoimg.webp");
  background-size: contain;
}

.login-box{
  display: flex;
  width: 35%;
  height: 100%;
  right: 0;
  position: absolute;
  align-items: center;
}
</style>

<!--  background-size: contain;   无论容器的大小如何变化，背景图都会缩放至能够完全适应容器，而不会拉伸或变形。-->